/**
*	addEvent(elem, "click", function() { alert('hi') })
*/
var addEvent, removeEvent;
if (document.addEventListener) { // проверка существования метода
    addEvent = function(elem, type, handler, capture) {
        elem.addEventListener(type, handler, capture ? true : false)
    }
    removeEvent = function(elem, type, handler, capture) {
        elem.removeEventListener(type, handler, capture ? true : false)
    }
} else {
    addEvent = function(elem, type, handler) {

		var scope = elem._listeners || {},
			listeners = scope[ type ] = scope[ type ] || [],
			proxy = function() {
				handler.call( elem, window.event );
			}
			
		listeners[ listeners.length ] = {
			original: handler,
			proxy: proxy
		}
		elem._listeners = scope;
		
        elem.attachEvent("on" + type, proxy);
    }
    removeEvent = function(elem, type, handler) {
		
		var scope = elem._listeners || {},
			listeners = scope[ type ] || [];
			
		for( var i = listeners.length - 1; i >= 0; i-- ) {
			if ( listeners[ i ].original === handler ) {
				elem.detachEvent( "on" + type, listeners[ i ].proxy );
				listeners.splice( i, 1 );
				break;
			}
		}
    }
}

function toSource( obj, max_recours, onlyOwnProps, tabN ) {
	onlyOwnProps = onlyOwnProps!=undefined ? onlyOwnProps : true ;//только свои свойства
	max_recours = max_recours || 4;// максимальная рекурсия
	tabN = tabN || 2; // отсчитываем табуляцию
	var tabstr = '\t', tab = new Array( tabN ).join(tabstr), tab_1 = new Array( tabN+1 ).join(tabstr);
	
	/* prevent "Maximum call stack size exceeded" */
	if( tabN-2 > max_recours ) return '{\n'+ tab_1 +
		'Recursion exceeded maximum value '+max_recours+'(second parameter of function)...\n'+tab+
	'}';
		
	var source = '{\n';
	for ( var i in obj ) {
		if ( obj.hasOwnProperty( i ) || !onlyOwnProps ) {
			if(typeof obj[i]==='object' )
			source += tab_1 + i + ': ' + toSource( obj[i], max_recours, onlyOwnProps, tabN+1 ) + ',\n';
			else
			source += tab_1 + i + ': ' + obj[i] + ',\n';
		}
	}
	return source + tab +'}';
};
/*
function getRequestBody( form ) { 
	var aParams = new Array();
	for(var i = 0; i < form.elements.length; i++) {
		var sParam = encodeURIComponent( form.elements[i].name );
		sParam += "=";
		sParam += encodeURIComponent( form.elements[i].value );
		aParams.push(sParam);
	}
	return aParams.join("&");
}*/
